package com.example.administrator.exkotlin.contentprovider

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.ActivityCompat
import android.widget.Toast
import com.example.administrator.exkotlin.R
import kotlinx.android.synthetic.main.activity_permision.*
import androidx.core.app.ComponentActivity
import androidx.core.app.ComponentActivity.ExtraData
import android.icu.lang.UCharacter.GraphemeClusterBreak.T




/**
 * 运行时权限
 * 隐式跳转到设置相关界面
 * */
class PermisionActivity : AppCompatActivity() {
    val Permissions = arrayOf(android.Manifest.permission.CALL_PHONE)
    val mRquestCode = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_permision)
        initDatas()
    }

    private fun initDatas() {

        btn_call.setOnClickListener {
            //检查并请求权限
            if (requstPermisions(Permissions)) {
                call()
            }
        }
        btn_wifi.setOnClickListener {
            goHuaWeiWifi()
        }
    }

    private fun requstPermisions(permissions: Array<String>): Boolean {
        var result = true
        for (permission in permissions) {
            if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                result = false
            }
        }
        if (!result) {
            ActivityCompat.requestPermissions(this, Permissions, mRquestCode)
        }

        return result
    }

    @SuppressLint("MissingPermission")
    private fun call() {
        val callIntent = Intent(Intent.ACTION_CALL)
        callIntent.data = Uri.parse("tel:123123")
        startActivity(callIntent)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            mRquestCode -> {
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    call()
                } else {
                    //是否完全禁止弹出权限
                    if (!shouldShowRequestPermissionRationale(Permissions)) {
                        Toast.makeText(this, "请自行到设置中，手动打开权限", Toast.LENGTH_SHORT).show()
                        goHuaWeiMainager()
                    } else {
                        Toast.makeText(this, "请再次同意权限才能使用拨号功能", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }

    /**
     * 再次申请权限时，是否需要声明
     * 是否完全禁止弹出权限申请
     * @param permissions
     * @return
     */
    private fun shouldShowRequestPermissionRationale(permissions: Array<String>): Boolean {
        for (permission in permissions) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                return true
            }
        }
        return false
    }

    /**
     * 打开华为设置中的应用权限管理 ：
     * ActivityRecord{com.huawei.systemmanager/com.huawei.securitycenter.permission.ui.activity.MainActivity}}
     * */
    private fun goHuaWeiMainager() {
        try {
            val intent = Intent()
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            intent.component = ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity")
            startActivity(intent)
        } catch (e: Exception) {
            Toast.makeText(this, "跳转失败", Toast.LENGTH_LONG).show()
            e.printStackTrace()
        }
    }

    /**
     * 打开华为设置中wifi设置：
     * */
    private fun goHuaWeiWifi() {
        val wifiIntent = Intent()
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            //Honeycomb
            wifiIntent.setClassName("com.android.settings", "com.android.settings.Settings\$WifiSettingsActivity")
        } else {
            //这种方法适配大多数安卓版本
            wifiIntent.action="android.settings.WIFI_SETTINGS"
        }
        startActivity(wifiIntent)

    }

}
